﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Media;
using SpriteSheetRuntime;
using System.IO;

namespace Megaman
{
    class IntroScene : IDisposable
    {
        private Game game;
        private Texture2D logo;
        private Texture2D megaman;
        private SpriteFont font;
        public ContentManager Content
        {
            get { return content; }
        }
        ContentManager content;
        private int AlphaValue = 1;
        private int fadeIncrement = 3;
        float fadeDelay = 0.035f;



        public IntroScene(IServiceProvider service, Game game)
        {
            content = new ContentManager(service);
            content.RootDirectory = "Content";
            this.game = game;
            logo = content.Load<Texture2D>("Intro/IntroTexture");
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(content.Load<Song>("Intro/IntroSong"));
            megaman = content.Load<Texture2D>("Intro/MegamanX");
            font = content.Load<SpriteFont>("Intro/Hud");

        }

        
        public float time = 0;
        public void Update(GameTime gameTime)
        {
            HandleInput();


        }

        public void HandleInput()
        {
            GamePadState gpad = GamePad.GetState(PlayerIndex.One);
            KeyboardState state  = Keyboard.GetState();
            if (state.IsKeyDown(Keys.Enter) || gpad.IsButtonDown(Buttons.Start ))
            {
                MediaPlayer.Stop();
                game.LoadStages();
            }

        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            time += (float)gameTime.ElapsedGameTime.TotalSeconds;
            spriteBatch.Begin();

            if (time > 2)
            {
                if (AlphaValue < 255)
                {
                    fadeDelay -= (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (fadeDelay < 0)
                    {
                        fadeDelay = 0.035f;

                        AlphaValue += fadeIncrement;
                    }
                }
                if (AlphaValue > 255)
                    AlphaValue = 255;


                spriteBatch.Draw(logo, new Rectangle(0, 0, spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height), new Color(255, 255, 255, (byte)AlphaValue));
            }

            if (time > 24)
            {
                spriteBatch.Draw(megaman, new Rectangle((spriteBatch.GraphicsDevice.Viewport.Width - megaman.Width) / 2, spriteBatch.GraphicsDevice.Viewport.Height/2 - megaman.Height/4, megaman.Width, megaman.Height), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0);
                if(((int)(time) % 2) == 0 && time > 28)
                    spriteBatch.DrawString(font, "Press Start", new Vector2(580, 600), Color.White,0,new Vector2(0,0),1f,SpriteEffects.None,0);
                    
            }
            
            spriteBatch.End();
        }
        


        #region IDisposable Members

        public void Dispose()
        {
            content.Unload();
        }

        #endregion
    }
}
